package com.centaline.androidsalesblog.api.usercenter;

import com.android.volley.Request;
import com.centaline.androidsalesblog.api.UserCenterApi;
import com.centaline.androidsalesblog.constants.AppConstant;
import com.centanet.framework.interfaces.IResponseListener;
import com.orhanobut.logger.Logger;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 短信接口基类
 * Created by guilin on 16/3/18.
 */
public abstract class BaseSmsApi<T> extends UserCenterApi<T> {

    final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    final String publicSecret = "F53DB798524C44AF8DE106DB744EC07";
    final String privateSecret = "2Fo1URzMx2qw3zV2juhcvM7LfjG";

    protected String mobile;

    public BaseSmsApi(Class<?> tag, Class<T> aClass, IResponseListener<T> iResponseListener) {
        super(tag, aClass, iResponseListener);
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    @Override
    protected int getMethod() {
        return Request.Method.POST;
    }

    @Override
    protected void setParams() {
        resetParams();
        putParams("AppNo", "APP");
        putParams("CityCode", AppConstant.CityCode);
        putParams("Mobile", mobile);
    }

    protected String md5(String... strings) {
        final StringBuilder input = new StringBuilder();
        for (String s : strings) {
            input.append(s);
        }
        Logger.d("md5 : %s", input.toString());
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(input.toString().getBytes());
            byte[] bytes = messageDigest.digest();
            int len = bytes.length;
            char[] resultCharArray = new char[len * 2];
            int index = 0;
            for (byte b : bytes) {
                resultCharArray[index++] = HEX_DIGITS[b >>> 4 & 0xf];
                resultCharArray[index++] = HEX_DIGITS[b & 0xf];
            }
            return new String(resultCharArray);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}
